function initGridPanel(obj)
%INITGRIDPANEL initializes grid panel

% This file is part of CISNE.
%
%     CISNE is free software: you can redistribute it and/or modify
%     it under the terms of the GNU General Public License as published by
%     the Free Software Foundation, either version 3 of the License, or
%     any later version.
%
%     CISNE is distributed in the hope that it will be useful,
%     but WITHOUT ANY WARRANTY; without even the implied warranty of
%     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
%     GNU General Public License for more details.
%
%     You should have received a copy of the GNU General Public License
%     along with CISNE.  If not, see <http://www.gnu.org/licenses/>.

% Copyright 2011. Jose Maria Garcia-Valdecasas Bernal


data=obj.loadData();

for j=1:size(data,1)
    prop=com.jidesoft.grid.DefaultProperty();
    prop.setName(data{j,1});
    if strcmp(data{j,3},'num')
        prop.setType(obj.m_jnum);
        prop.setValue(data{j,4});
    
    elseif strcmp(data{j,3},'listbox')
        prop.setType(obj.m_jstr);
        options = data{j,4};
        editor = com.jidesoft.grid.ListComboBoxCellEditor(options);
        context = getContext('combobox');
        com.jidesoft.grid.CellEditorManager.registerEditor(obj.m_jstr, ...
            editor, context);
        prop.setEditorContext(context);
        prop.setValue(options{1});
       
    elseif strcmp(data{j,3},'int')
        prop.setType(obj.m_jint);
        prop.setValue(int32(data{j,4}));
        
    elseif strcmp(data{j,3},'str')
        prop.setType(obj.m_jstr);
        prop.setValue(data{j,4});
    
    elseif strcmp(data{j,3},'vector')
        prop.setType(obj.m_jstr);
        prop.setEditable(false);
        
        children=data{j,4};
        for k=1:length(children)
            child=com.jidesoft.grid.DefaultProperty();
            child.setName(children{k});
            child.setType(obj.m_jnum);
            prop.addChild(child);
        end
    elseif strcmp(data{j,3},'dir')
        prop.setType(obj.m_jstr);
        com.jidesoft.grid.CellEditorManager.registerEditor(obj.m_jstr,...
            com.jidesoft.grid.FolderCellEditor,...
            com.jidesoft.grid.FolderCellEditor.CONTEXT);
        prop.setEditorContext(com.jidesoft.grid.FolderCellEditor.CONTEXT);
    elseif strcmp(data{j,3},'bool')
        prop.setType(obj.m_jbool);
        com.jidesoft.grid.CellEditorManager.registerEditor(obj.m_jbool,...
            com.jidesoft.grid.BooleanCheckBoxCellEditor,...
            com.jidesoft.grid.BooleanCheckBoxCellEditor.CONTEXT);
        prop.setEditorContext(com.jidesoft.grid.BooleanCheckBoxCellEditor.CONTEXT)
        prop.setValue(data{j,4});

    end
    
    if isempty(obj.m_gridmodel.getProperty(data{j,5}))
        prop.setCategory(data{j,5});
        obj.m_gridList.add(prop);
    else
        parent=obj.m_gridmodel.getProperty(data{j,5});
        parent.addChild(prop);
        prop.setCategory(parent.getCategory);
    end
    
    prop.setDisplayName(data{j,1});
    prop.setDescription(data{j,2});
    prop.setEditable(true);
    
end
obj.m_gridmodel.refresh();
obj.m_gridmodel.expandAll();
obj.m_gridmodel.refresh();
end

function Context=getContext(editorType)
persistent ContextCounter;
if isempty(ContextCounter)  % if not yet assigned
   ContextCounter = 1;
end

Context = com.jidesoft.grid.EditorContext(['JIDE_' editorType '_' int2str(ContextCounter)]);

ContextCounter = ContextCounter+1;
end


